package util;

import cn.jiguang.common.DeviceType;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.jfinal.kit.PropKit;


/**
 * Created by Administrator on 2016/12/8 0008.
 */
public class PushConsumer {
    private static int index = 1;

    public static String pushName = PropKit.get("push.name", "成都教育");
    public static String pushSecret = PropKit.get("push.secret", "32aadf3a633b9410fa079e2f");
    public static String appKey = PropKit.get("push.key", "5ac94a50f68d11b7d66a49a5");

/*    public static void send(String tag, String title, String description, Long id) {
        JPushClient jpush = new JPushClient(pushSecret, pushKey);
        IOSExtra iosExtra = new IOSExtra(1, "Windows_Logon_Sound.wav");//badge and sound

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("ios", iosExtra);
        jpush.sendNotificationWithTag(index++, tag, title == null ? pushName : title, description, 0, map);
    }*/

    public static void send(String tag, String description, Long id) {

        JPushClient jpushClient = new JPushClient(pushSecret, appKey);

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.newBuilder()
                        .addDeviceType(DeviceType.Android)
                        .addDeviceType(DeviceType.IOS).build())
                .setAudience(Audience.tag(tag))
                /*.setMessage(Message.newBuilder()
                        .setMsgContent(description)
                        .addExtra("id", id)
                        .build())*/
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(description)
                                .setBadge(1)
                                .setSound("Windows_Logon_Sound.wav")
                                .addExtra("studentId", id)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(pushName)
                                .setAlert(description)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();

        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);

        } catch (Exception e) {
            // Should review the error, and fix the request
            e.printStackTrace();
        }
    }
}
